package com.ugoodtech.mdcc.web.controller;

import com.google.common.collect.Lists;
import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.Brand;
import com.ugoodtech.mdcc.core.domain.Department;
import com.ugoodtech.mdcc.core.domain.Nation;
import com.ugoodtech.mdcc.core.domain.QNation;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.domain.UserProfile;
import com.ugoodtech.mdcc.core.repository.NationRepository;
import com.ugoodtech.mdcc.core.repository.UserProfileRepository;
import com.ugoodtech.mdcc.web.dto.ServerResponse;
import com.ugoodtech.mdcc.web.exception.BizException;
import com.ugoodtech.mdcc.web.exception.EnumBizError;
import com.ugoodtech.mdcc.web.pojo.vo.PerformanceReportStatisticsResponse;
import com.ugoodtech.mdcc.web.service.PerformanceReportStatisticsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;


/**
 * Created by Mario on 2020/2/17.
 * 业绩报告统计
 * @author Mario
 */
@RestController
@RequestMapping("/v1/performanceReportStatistics")
public class PerformanceReportStatisticsController extends BaseController{
	@Autowired
	private PerformanceReportStatisticsService performanceReportStatisticsService;

	@Resource
	private NationRepository nationRepository;

	@Resource
	private UserProfileRepository userProfileRepository;

	/**
	 * 获取业绩报告数据统计列表接口
	 * @param province
	 * @param brandType
	 * @param customerType
	 * @param department
	 * @param brand
	 * @param name
	 * @param year
	 * @param month
	 * @return PerformanceReportStatisticsResponse performanceReportStatisics
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	public ServerResponse getPerformanceReportStatisicsList(@RequestParam(value = "province",defaultValue = "")   String province,
															@RequestParam(value = "brandType",defaultValue = "") String brandType,
															@RequestParam(value = "customerType",defaultValue = "")  String customerType,
															@RequestParam(value = "department",defaultValue = "")  String department,
															@RequestParam(value = "brand",defaultValue = "")  String brand,
															@RequestParam(value = "employeeName",defaultValue = "")  String name,
															@RequestParam(value = "year",defaultValue = "2020")  Integer year,
															@RequestParam(value = "month",defaultValue = "")  String month){
//		isUserLogin();
		User currentUser = getCurrentUser();
		UserProfile userProfile;
		if (currentUser==null){
			userProfile = userProfileRepository.findByUserId(571l);
		}else {
			userProfile = userProfileRepository.findByUserId(currentUser.getId());
		}
		List<Long> userProvinceIdList =new ArrayList<>();//用户的地区权限
		List<Long> userBrandIdList =new ArrayList<>();//用户的品牌权限
		List<String> provinceList = new ArrayList<>();//省份列表
		List<Long> provinceIdList = new ArrayList<>();//省份Id列表
		List<Integer> brandTypeList = new ArrayList<>();//品牌列表
		List<Integer> customerTypeList = new ArrayList<>();//客户类型列表
		List<Long> departmentList = new ArrayList<>();//部门列表
		List<Long> brandList = new ArrayList<>();//品牌列表
		List<Integer> monthList = new ArrayList<>();//月份列表
		if(!province.equals("")){
			List<String> stringList = Arrays.asList(province.split(","));//拆分输入项获取省份列表，用英文逗号分
			for(String string : stringList){
				try{
					provinceList.add(string);
				}catch (NumberFormatException numberFormatException){
					throw new BizException(EnumBizError.PARAMS_INVALID.setErrMsg("省份请传编号"));
				}
			}
		}
		List<Nation> userNationList = userProfile.getNationList();
		for (Nation nation : userNationList) {
			userProvinceIdList.add(nation.getId());
		}
		if(provinceList.size() > 0){
			BooleanBuilder builder = new BooleanBuilder();
			QNation qNation =QNation.nation;
			builder.and(qNation.province.in(provinceList));
//			LocationDOExample locationDOExample = new LocationDOExample();
//			locationDOExample.or().andProvinceIn(provinceList);
//			List<Nation> locationDOList = locationDOMapperExt.selectByExample(locationDOExample);

			Iterable<Nation> all = nationRepository.findAll(builder);
			List<Nation> locationDOList = Lists.newArrayList(all);
			if(locationDOList.size() > 0){
					for(Nation locationDO : locationDOList){
							provinceIdList.add(locationDO.getId());
					}
			}
		}
		if(!brandType.equals("")){
			List<String> stringList = Arrays.asList(brandType.split(","));//拆分输入项获取品牌列表，用英文逗号分
			for(String string : stringList){//转化品牌类型集合为Integer类型
				try{
					Integer i = Integer.parseInt(string);
					brandTypeList.add(i);
				}catch (NumberFormatException numberFormatException){
					throw new BizException(EnumBizError.PARAMS_INVALID.setErrMsg("品牌类型请传0，1"));
				}
			}
		}
		if(!customerType.equals("")){
			List<String> stringList = Arrays.asList(customerType.split(","));//拆分输入项获取客户类型
			for(String string : stringList){//转化客户类型集合为Integer类型
				try{
					Integer i = Integer.parseInt(string);
					customerTypeList.add(i);
				}catch (NumberFormatException numberFormatException){
					throw new BizException(EnumBizError.PARAMS_INVALID.setErrMsg("客户类型请传0，1"));
				}
			}
		}
		List<Department> userDepartmentList = userProfile.getDepartmentList();
		if(!department.equals("")){
			List<String> stringList = Arrays.asList(department.split(","));//拆分输入项获取部门列表，用英文逗号分
			for (Department department1 : userDepartmentList) {
				for(String string : stringList){
					Long i = Long.parseLong(string);
					if (department1.getId().longValue()==i){{//筛选出用户权限中可以看到的部门
						departmentList.add(i);
					}
				}
			}
			}
		}else {
			//未传入部门，查询用户权限中可以看到的部门
			for (Department department1 : userDepartmentList) {
				departmentList.add(department1.getId());
			}
		}

		List<Brand> userBrandList = userProfile.getBrandList();
		for (Brand brand1 : userBrandList) {
			userBrandIdList.add(brand1.getId());
		}
		if(!brand.equals("")){
			List<String> stringList = Arrays.asList(brand.split(","));
				for(String string : stringList){
					Long i = Long.parseLong(string);
						brandList.add(i);
			}
		}
		if(!month.equals("")){
			List<String> stringList = Arrays.asList(month.split(","));//拆分输入项获取月份
			for(String string : stringList){//转化月份集合为Integer类型
				Integer i = Integer.parseInt(string);
				monthList.add(i);
			}
		}
		PerformanceReportStatisticsResponse performanceReportStatisics = performanceReportStatisticsService.getPerformanceReportStatistics(brandTypeList,customerTypeList,name,monthList,year,departmentList,provinceIdList,brandList,userProvinceIdList,userBrandIdList);
		return ServerResponse.success("获取数据统计列表成功",performanceReportStatisics);
	}
}
